package com.yixun.qfbao.hander;

import com.alibaba.fastjson.JSONObject;
import com.yixun.qfbao.apidto.RedisOperateReqDto;
import com.yixun.qfbao.apidto.RedisOperateResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.util.RedisUtil;
import com.yixun.qfbao.vo.RedisOperateReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * redis 操作
 * 操作类型: optType
 * 类型描述: 1: 新增, 2: 查询, 3: 删除
 * @author JHSong
 *		
 */
@Service("RedisOperateHander")
public class RedisOperateHander extends CommonAbstractHander {

	private Logger logger = LoggerFactory.getLogger(RedisOperateHander.class);

	@SuppressWarnings("rawtypes")
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		RedisOperateReqVo req = (RedisOperateReqVo)reqDto;
		RedisOperateReqDto reqDto1 = req.getParams();
		BaseResponseDTO<RedisOperateResDto> res = new BaseResponseDTO<RedisOperateResDto>();
		RedisOperateResDto resDto = new RedisOperateResDto();
		try {
			Object result = new Object();
			if("1".equals(reqDto1.getOptType())){
				RedisUtil.set(reqDto1.getKey(), JSONObject.toJSONString(reqDto1.getValue()));
			}else if("2".equals(reqDto1.getOptType())){
				result = RedisUtil.get(reqDto1.getKey());
			}else if("3".equals(reqDto1.getOptType())){
				RedisUtil.delete(reqDto1.getKey());
			}
			resDto.setResult(JSONObject.toJSONString(result));
			res.setResult(ResultEnum.SUCCESS.getNo());
			res.setResData(resDto);
		} catch (Exception e) {
			logger.error("redis 操作失败,操作类型: {}", e);
			resDto.setResultMsg("redis 操作失败,操作类型: " + reqDto1.getOptType() + ",失败结果: " + e.getMessage());
		}
		res.setResData(resDto);
		res.setResult(resDto.getResult());
		return res;
	}
	
}
